package com.rainyun.rd.common;

import com.rainyun.rc.command.CmdUtil;
import com.rainyun.rc.common.VersionException;
import com.rainyun.rc.config.ErrorHandler;
import com.rainyun.rc.config.NodeConfig;
import com.rainyun.rc.ioc.Beanzy;
import com.rainyun.rc.ioc.RainIOC;
import com.rainyun.rc.orm.RainDB;
import com.rainyun.rc.orm.SqliteDB;
import com.rainyun.rcmc.bukkit.config.BukYmlConfig;
import com.rainyun.rcmc.bukkit.config.BukYmlNode;
import com.rainyun.rcmc.own.bukkit.controller.CmdRC;
import com.rainyun.rcmc.own.bukkit.main.BukRainCore;
import com.rainyun.rd.config.RdConfig;
import com.rainyun.rd.config.RdLang;
import com.rainyun.rd.entity.DogInfo;
import com.rainyun.rd.entity.PlayerInfo;
import com.rainyun.rd.main.RainDemo;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Logger;

/**全局通用方法/业务*/
@Beanzy
public class RdGlobal {

    @BeanRd(BeanRd.BeansRd.Logger)
    Logger logger;
    @Beanzy
    CmdRC cmdRC;

    @BeanRd(BeanRd.BeansRd.Classloader)
    ClassLoader classloader;

    //插件主类
    @Beanzy
    RainDemo plugin;

    /**插件的重载方法*/
    public boolean BukReload() {

        //全局错误处理器(配置)
        ErrorHandler eh = (path, e)->{
            logger.info(NodeConfig.getDefaultByEnum(RdLang.reload_error).toString());
            e.printStackTrace();
        };

        //配置管理器(对象储存)
        BukYmlConfig configManger = new BukYmlConfig(plugin.getDataFolder());
        configManger.setErrorHandler(eh);
        configManger.add(RdConfig.class,"config");
        RainIOC.set(BeanRd.BeansRd.ObjConf,configManger);
        try {
            configManger.loadAllOrNew();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        //存入核心配置
        RdConfig config = configManger.get(RdConfig.class);
        RainIOC.set(BeanRd.BeansRd.Config,config);

        //配置管理器(节点/枚举节点储存)
        NodeConfig<YamlConfiguration> nodeManger = new BukYmlNode(plugin.getDataFolder(),"",classloader);
        nodeManger.setErrorHandler(eh);
        nodeManger.add("langfiles", "lang/")
                .add("lang","lang/"+config.getLang(),RdLang.class);
        nodeManger.setResource("langfiles",true);
        RainIOC.set(BeanRd.BeansRd.NodeConf,nodeManger);
        try {
            nodeManger.loadAllOrNew();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        //自动生成指令帮助的配置树
        YamlConfiguration lang = nodeManger.get("lang");
        ConfigurationSection cmd = lang.getConfigurationSection("cmds");
        Map<String, Object> tree = BukYmlNode.confToMap(cmd);
        CmdUtil.setAttrByTree(RainIOC.get(BeanRd.BeansRd.Cmds),tree,"help");

        //数据库管理器
        RainDB dbman = new SqliteDB(plugin.getDataFolder().getAbsolutePath()+"/data");
        RainIOC.set(BeanRd.BeansRd.DBMan,dbman);

        dbman.setDebug(config.isDebug());
        //导入2个实体类
        dbman.addClazz(PlayerInfo.class,"rd_player_info");
        dbman.addClazz(DogInfo.class,"rd_dog_info");
        try {
            //初始化
            dbman.initAll();
        } catch (VersionException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


}
